<?php
namespace common\components;

use Yii;
use JPush\Model as M;
use JPush\JPushClient;
use JPush\JPushLog;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

use JPush\Exception\APIConnectionException;
use JPush\Exception\APIRequestException;


/**
 * 作者：d.coffee
 * 时间：2015-11-23
 * 功能：极光推送 API
 */
class Jpush {
    public $client = null;

    public function __construct()
    {
		$app_key=Yii::$app->params['Jpush']['app_key'];
		$master_secret = Yii::$app->params['Jpush']['master_secret'];
		$this->client = new JPushClient($app_key, $master_secret);
    }
    /**
     * 根据别名alias单个推送消息
     * alias 用户标签(唯一)
     * message 通知消息内容
     * message_content 应用内消息 内容体
     * extras 拓展信息
     */
	public function PushAlias($alias = array(), $message = '', $message_content = '', $extras = array())
    {

        if (!$alias || !$message) {
            $data = [
                'msg' => '用户别名或消息内容必须',
                'status' => 2,
                'code' => 400,
			];
            return $data;
        }


		try {
			$result = $this->client->push()
				->setPlatform(M\platform('ios', 'android'))      
				->setAudience(M\alias($alias))      
				->setNotification(M\notification($message, M\android($message), M\ios($message, 'happy', 1, true, null, 'THE-CATEGORY')))
                ->setMessage(M\message($message_content, null, null, $extras))
				->send();
            if ( $result->isOk ) {
                $data = [
                    'msg' => '发送成功',
                    'status' => 2,
                    'code' => 200,
                ];
            }

		} catch (APIRequestException $e) {
			$data = [
                'msg' => $e->message,
                'status' => 0,
                'code' => $e->httpCode,
			];
		} 
		return $data;
	}

    /**
     * 根据标签tag群推消息
     */
    public function PushTag($alias = array(), $message = '', $message_content = '', $extras = array())
    {

		if (!$tag || !$message) {
            $data = [
                'msg'=>'标签名或消息内容必须',
                'status'=>2,
                'code'=>400
			];
            return $data;
        }
       

		try {
			$result = $this->client->push()
				->setPlatform(M\platform('ios', 'android'))      
				->setAudience(M\Tag($tag))      
				->setNotification(M\notification($message, M\android($message), M\ios($message, 'happy', 1, true, null, 'THE-CATEGORY')))
                ->setMessage(M\message($message_content, null, null, $extras))
				->send();
            if ( $result->isOk ) {
                $data = [
                    'msg'=>'发送成功',
                    'status'=>2,
                    'code'=>200
                ];
            }

		} catch (APIRequestException $e) {
			$data = [
                'msg'=>$e->message,
                'status'=>0,
                'code'=>$e->httpCode
			];   
		} 
		return $data;
	}

    
    /**
     * 广播所有用户推送消息
     */
    public function PushAll( $message = '' )
    {
		try {
            $result = $this->client->push()
            ->setPlatform(M\all)
            ->setAudience(M\all)
            ->setNotification(M\notification($message))
            ->send();
            if ( $result->isOk ) {
                $data = [
                    'msg'=>'发送成功',
                    'status'=>2,
                    'code'=>200
                ];
            }
			
			
		} catch (APIRequestException $e) {
		
			$data = [
                'msg'=>$e->message,
				'status'=>0,
				'code'=>$e->httpCode
			];
			
		}
        return $data;
		
	}



	
}